package ar.edu.itba.it.pdc.configuration;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.Logger;

public class ProxyConfigurationLoader {

	private static final ProxyConfigurationLoader INSTANCE = new ProxyConfigurationLoader();
	
	private final Logger logger = Logger.getLogger(ProxyConfigurationLoader.class);
	
	public static final int DEFAULT_PROXY_PORT = 8081;
	public static final int DEFAULT_MONITORING_PORT = 2020;
	public static final int DEFAULT_CONFIG_PORT = 2021;
	
	public static final String DEFAULT_USERNAME = "pdc";
	public static final String DEFAULT_PASSWORD = "pdcpass";
	
	public static final int DEFAULT_BUFFER_CANT = 2 * 1024;
	public static final int DEFAULT_BUFFER_MAX = 4 * 1024;
	public static final int DEFAULT_BUFFER_SIZE = 4 * 1024;
	
	private Properties properties;
	private Properties defaultProps;
	
	private ProxyConfigurationLoader() {

		defaultProps = new Properties();
		
		defaultProps.setProperty("proxy_ports", Integer.toString(DEFAULT_PROXY_PORT));
		defaultProps.setProperty("monitoring_ports", Integer.toString(DEFAULT_MONITORING_PORT));
		defaultProps.setProperty("config_ports", Integer.toString(DEFAULT_CONFIG_PORT));
		
		defaultProps.setProperty("username", DEFAULT_USERNAME);
		defaultProps.setProperty("password", DEFAULT_PASSWORD);
		
		defaultProps.setProperty("buffer_cant", Integer.toString(DEFAULT_BUFFER_CANT));
		defaultProps.setProperty("buffer_max", Integer.toString(DEFAULT_BUFFER_MAX));
		defaultProps.setProperty("buffer_size", Integer.toString(DEFAULT_BUFFER_SIZE));
				
		properties = new Properties(defaultProps);

		try{
			FileInputStream in  = new FileInputStream("./src/main/resources/proxy.properties");
			properties.load(in);
			in.close();
			logger.debug("Using custom file proxy configuration.");
		} catch (IOException e) {
			logger.debug("Using default proxy configuration.");
		}

	}
	
	public static ProxyConfigurationLoader getInstance(){
		return INSTANCE;
	}
	
	public String getValue(String propName){
		return properties.getProperty(propName);
	}
	
	public Integer getValueAsInteger(String propName){
		try{
			return Integer.parseInt(properties.getProperty(propName));
		} catch(NumberFormatException nfe){
			logger.warn("The value of " + propName +
					" must be an integer. Using default value");
			return Integer.parseInt(defaultProps.getProperty(propName));
		}
	}
	
	public List<Integer> getValuesAsInteger(String propName){
		String value = properties.getProperty(propName);
		List<String> values = Arrays.asList(value.split(","));
		List<Integer> answer = new ArrayList<Integer>();
		try{
			for(String s: values){
				answer.add(Integer.parseInt(s.trim()));
			}
		} catch(NumberFormatException nfe){
			logger.warn("The value of " + propName +
					" must be a comma separated list of integers. Using default values");
			answer = new ArrayList<Integer>();
			answer.add(Integer.parseInt(defaultProps.getProperty(propName)));
		}
		return answer;
	}
	
}
